package com.beep.erp.model.update;

import android.content.Context;
import android.content.Intent;
import android.support.annotation.NonNull;
import android.widget.Toast;

import com.afollestad.materialdialogs.DialogAction;
import com.afollestad.materialdialogs.MaterialDialog;
import com.beep.erp.AppContext;
import com.beep.erp.R;
import com.beep.erp.common.utils.AppUtils;
import com.beep.erp.model.update.service.DownLoadService;

/**
 * FileName: UpdateManager
 * description:
 */
public class UpdateManager {

    private Context mContext;

    public UpdateManager(Context context) {
        this.mContext = context;
    }

    @SuppressWarnings("unused")
    public static void checkUpdate(boolean isAutoInstall, UpdateCallback updateCallback) {
        if (!isAutoInstall)
            return;
        /**
         * 在这里请求后台接口，获取更新的内容和最新的版本号
         */
        // 版本的更新信息
        String version_info = AppContext._context.getString(R.string.update_content);
        int mVersion_code = AppUtils.getVersionCode(AppContext.context());
        int nVersion_code = 2;
        if (mVersion_code < nVersion_code) {
            // 显示提示对话
            showNoticeDialog(version_info);
        } else {

            Toast.makeText(AppContext.context(),AppContext._context.getString(R.string.the_new), Toast.LENGTH_SHORT).show();

        }
    }

    /**
     * 显示更新对话框
     *
     * @param version_info
     */
    private static void showNoticeDialog(String version_info) {
        new MaterialDialog.Builder(AppContext.context())
                .title(AppContext._context.getString(R.string.update_mes))
                .content(version_info)
                .positiveText(AppContext._context.getString(R.string.update_now))
                .negativeText(AppContext._context.getString(R.string.update_later))
                .onPositive(new MaterialDialog.SingleButtonCallback() {
                    @Override
                    public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {
                        dialog.dismiss();
                        AppContext.context().startService(new Intent(AppContext.context(), DownLoadService.class));
                    }
                })

                .onNegative(new MaterialDialog.SingleButtonCallback() {
                    @Override
                    public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {
                        dialog.dismiss();
                    }
                }).show();

    }
}
